package org.farm2.skc.service.impl;

import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.farm2.base.db.FarmDbFields;
import org.farm2.base.domain.FarmUserContextLoader;
import org.farm2.base.exception.FarmAppException;
import org.farm2.base.exception.FarmExceptionUtils;
import org.farm2.base.password.FarmPasswordEncoder;
import org.farm2.skc.dao.SkcKnowTagDao;
import org.farm2.skc.domain.SkcKnowTag;
import org.farm2.skc.service.SkcKnowTagServiceInter;
import org.farm2.tools.bean.FarmBeanUtils;
import org.farm2.tools.db.DataQuery;
import org.farm2.tools.db.DataResult;
import org.farm2.tools.i18n.I18n;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.List;

/**知识标签 
 * @author cbtg自动生成  2025-2-11 15:16:30 
 */
@Service
@Slf4j
public class SkcKnowTagServiceImpl implements SkcKnowTagServiceInter {


    @Autowired
    private SkcKnowTagDao skcKnowTagDao;

    @Transactional
    @Override
    public SkcKnowTag insertSkcKnowTagEntity(SkcKnowTag skcKnowTag) {
        FarmDbFields.initInsertBean(skcKnowTag, FarmUserContextLoader.getCurrentUser());

        skcKnowTagDao.insert(skcKnowTag);


        return skcKnowTag;
    }

    @Transactional
    @Override
    public SkcKnowTag editSkcKnowTagEntity(SkcKnowTag skcKnowTag) {
        SkcKnowTag saveSkcKnowTag = getSkcKnowTagById(skcKnowTag.getId());
        FarmExceptionUtils.throwNullEx(saveSkcKnowTag, I18n.msg("知识标签不存在:?", skcKnowTag.getId()));
        saveSkcKnowTag.setId(skcKnowTag.getId());
        saveSkcKnowTag.setCtime(skcKnowTag.getCtime());
        saveSkcKnowTag.setCuserkey(skcKnowTag.getCuserkey());
        saveSkcKnowTag.setState(skcKnowTag.getState());
        saveSkcKnowTag.setNote(skcKnowTag.getNote());
        saveSkcKnowTag.setTagkey(skcKnowTag.getTagkey());
        saveSkcKnowTag.setType(skcKnowTag.getType());
        saveSkcKnowTag.setTitle(skcKnowTag.getTitle());
        saveSkcKnowTag.setKnowid(skcKnowTag.getKnowid());
         
        FarmDbFields.initUpdateBean(saveSkcKnowTag, FarmUserContextLoader.getCurrentUser());
        skcKnowTagDao.update(saveSkcKnowTag);
        return saveSkcKnowTag;
    }

    @Transactional
    @Override
    public SkcKnowTag getSkcKnowTagById(String id) {
        return skcKnowTagDao.findById(id);
    }

    @Override
    public List<SkcKnowTag> getSkcKnowTags(DataQuery query) {
        return skcKnowTagDao.queryData(query.setCount(false)).getData(SkcKnowTag.class);
    }



    @Transactional
    @Override
    public DataResult searchSkcKnowTag(DataQuery query) {
        DataResult result = skcKnowTagDao.queryData(query);
        return result;
    }

    @Override
    public int getSkcKnowTagNum(DataQuery query) {
        return skcKnowTagDao.countData(query);
    }


    @Transactional
    @Override
    public void delSkcKnowTag(String id) {
        /*[tree：树形结构使用]
        if ( skcKnowTagDao.findByParentId(id).size() > 0) {
            throw new RuntimeException("不能删除该节点，请先删除其子节点");
        }
        */
        skcKnowTagDao.deleteById(id);
    }
    
    @Override
    public int getNum(DataQuery query) {
        return  skcKnowTagDao.countData(query);
    }
    
    /*[tree：树形结构使用]
    @Transactional
    @Override
    public void moveTreeNode(List<String> sourceIds, String targetId) {
        for (String sourceId : sourceIds) {

            SkcKnowTag node = getSkcKnowTagById(sourceId);
            if (!"NONE".equals(targetId)) {
                SkcKnowTag target = getSkcKnowTagById(targetId);
                if (target.getTreecode().indexOf(node.getTreecode()) >= 0) {
                    throw new RuntimeException("不能够移动到其子节点下!");
                }
            }
            node.setParentid(targetId);
            skcKnowTagDao.update(node);

            List<SkcKnowTag> list = skcKnowTagDao.findSubNodes(sourceId);
            for (SkcKnowTag treenode : list) {
                initTreeCode(treenode.getId());
            }
        }
    }*/
    
     /**[tree：树形结构使用]
      * 构造treecode字段
     * @param treeNodeId
    private void initTreeCode(String treeNodeId) {
        SkcKnowTag node = skcKnowTagDao.findById(treeNodeId);
        if (node.getParentid().equals("NONE")) {
            node.setTreecode(node.getId());
        } else {
            node.setTreecode(skcKnowTagDao.findById(node.getParentid()).getTreecode() + node.getId());
        }
        skcKnowTagDao.update(node);
    }
     */
    /* [tree：树形结构使用]
    @Transactional
    @Override
    public void autoSort(List<String> ids) {
        int sort = 0;
        for (String id : ids) {
            SkcKnowTag node =  skcKnowTagDao.findById(id);
            if (sort == 0) {
                sort = node.getSortcode();
            }
            node.setSortcode(sort++);
            skcKnowTagDao.update(node);
        }
    }*/
}
